import {
    getAccountProductInfo,
    getAccountExpireNotify,
    getIongInsuranceRechargeDetail,
    getInsuranceRecharge,
    getEleSignRechargeRecord,
    getadphone
} from '../../api/app'
import user from '../../utils/user'
const app = getApp()
import dayjs from 'dayjs'
Page({
    /**
     * 页面的初始数据
     */
    // https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManager39198c62-cbc0-4445-b1b7-5e84ac42a3be.png
    data: {
        isup: false,
        rechargelist: [
            [{
                text: '管理员个人充值',
                type: 'app',
                path: 'wx49625208931d29ec',
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManager12d74a5d-c6b1-4776-a1fb-8ee57c493531.png'
            }, {
                text: '公对公充值',
                path: '/packagePay/pages/recharge/transfer/transfer',
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManagerbb5af04a-d300-4f65-88ba-be669df3f7ff.png'
            }],
            [{
                text: '微信充值',
                type: 'InsureRechargePeriodZgrs',
                path: '/packageInsurance/pages/insurance-recharge-wechat/insurance-recharge-wechat',
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManager39198c62-cbc0-4445-b1b7-5e84ac42a3be.png'
            }, {
                text: '公对公充值',
                type: 'InsureRechargePeriodZgrs',
                path: '/packageInsurance/pages/insurance-recharge-gtg/insurance-recharge-gtg',
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManagerbb5af04a-d300-4f65-88ba-be669df3f7ff.png'
            }],
            [{
                text: '微信充值',
                path: '/packageInsurance/pages/insurance-recharge-wechat/insurance-recharge-wechat',
                type: 'undefined',
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManager39198c62-cbc0-4445-b1b7-5e84ac42a3be.png'
            }, {
                text: '公对公充值',
                path: '/packageInsurance/pages/insurance-recharge-gtg/insurance-recharge-gtg',
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManagerbb5af04a-d300-4f65-88ba-be669df3f7ff.png'
            }],
            [{
                text: '公对公充值',
                path: '/packageMicroInsurance/pages/insurance-recharge-gtg/insurance-recharge-gtg',
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManagerb62f6188-4d9d-45fa-a63d-ce63d585796f.png'
            }],
            [{
                text: '微信充值',
                path: '/packageContract/pages/contract-recharge-gtg/contract-recharge-gtg',
                img: 'https://bullet001.oss-cn-shanghai.aliyuncs.com/file/payManagerb62f6188-4d9d-45fa-a63d-ce63d585796f.png'
            }],
        ],
        isOpenPay: false,
        isPopupOpenPay: false,
        isShowTips: false, // 提示信息显示
        accountProductInfo: {}, // 卡包信息
        expirationTime: {}, // 到期时间
        isOpenWesure: '',
        isOpenPeriodInsur: '',
        isEleSign: '',
        tyxSign: '',
        percentage: '', // 进度条百分比
        currentTab: 0,
        pageSize: 10,
        pageNum: 1,
        hasNext: true,
        list: [],
        phonelist: []
    },
    switchTabc(e) {
        this.setData({
            currentTab: e.detail.current,
        })
    },
    switchTab(e) {
        this.setData({
            currentTab: e.detail.current,
        })
        this.setData({
            list: [],
            pageNum: 1,
            pageSize: 10,
            hasNext: true,
        })
        if (this.data.currentTab === 1) {
            this.getIongInsuranceRechargeDetail()
        } else if (this.data.currentTab === 2) {
            this.onGetInsuranceRecharge()
        } else if (this.data.currentTab === 3) {
            this.onGetInsuranceRechargec()
        } else if (this.data.currentTab === 4) {
            this.getEleSignRechargeRecord()
        }
    },
    onGetInsuranceRechargec() {
        if (!this.data.tyxSign) {
            return
        }
        // 日日保接口 和 团意保使用同一个接口 通过 insureCompanyType 字段来区分
        getInsuranceRecharge({
            insureCompanyType: 'insure_type_tyrb',
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }).then(res => {
            if (!res.hasNextPage) {
                this.setData({
                    hasNext: false
                })
            }
            const arr = []
            arr.push(...this.data.list)
            arr.push(...res.list.map(item => {
                item.payMoney = item.rechargeAmount
                return item
            }))
            this.setData({
                list: arr
            })
        })
    },
    onGetInsuranceRecharge() {
        if (!this.data.isOpenWesure) {
            return
        }
        // 日日保接口
        getInsuranceRecharge({
            insureCompanyType: user.insureCompanyType,
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        }).then(res => {
            if (!res.hasNextPage) {
                this.setData({
                    hasNext: false
                })
            }
            const arr = []
            arr.push(...this.data.list)
            arr.push(...res.list.map(item => {
                item.payMoney = item.rechargeAmount
                return item
            }))
            this.setData({
                list: arr
            })
        })
    },
    // 获取长期工充值列表
    async getIongInsuranceRechargeDetail() {
        if (!this.data.isOpenPeriodInsur) {
            return
        }
        const result = await getIongInsuranceRechargeDetail({
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        })
        if (!result.hasNextPage) {
            this.setData({
                hasNext: false
            })
        }
        const arr = []
        arr.push(...this.data.list)
        arr.push(...result.list.map((item) => {
            item.payMoney = item.rechargeAmount
            return item
        }))
        this.setData({
            list: arr
        })
    },
    async getEleSignRechargeRecord() {
        // 电子签
        const result = await getEleSignRechargeRecord({
            pageNum: this.data.pageNum,
            pageSize: this.data.pageSize
        })
        if (!result.hasNextPage) {
            this.setData({
                hasNext: false
            })
        }
        const arr = []
        arr.push(...this.data.list)
        arr.push(...result.list)
        this.setData({
            list: arr
        })
    },
    onLoad(options) {
        this.setData({
            isOpenWesure: !!user.isOpenWesure,
            isOpenPeriodInsur: !!user.isOpenPeriodInsur,
            isOpenPay: !!user.isOpenPaySalary,
            isEleSign: !!Number(options.eleSign),
            currentTab: options.currrent * 1
        })
        // if (options.currrent * 1 === 1) {
        //     this.getIongInsuranceRechargeDetail()
        // } else if (options.currrent * 1 === 2) {
        //     this.onGetInsuranceRecharge()
        // } else if (options.currrent * 1 === 3) {
        //     this.onGetInsuranceRechargec()
        // } else if (options.currrent * 1 === 4) {
        //     this.getEleSignRechargeRecord()
        // }
    },
    goNext(event) {
        if (this.data.currentTab === 1) {
            wx.navigateTo({
                url: '/packageInsurance/pages/insurance-gtg-success/insurance-gtg-success?id=' + event.currentTarget.dataset.id + '&type=' + 'InsureRechargePeriodZgrs',
            })
        } else if (this.data.currentTab === 2) {
            wx.navigateTo({
                url: '/packageInsurance/pages/insurance-gtg-success/insurance-gtg-success?id=' + event.currentTarget.dataset.id + '&type=' + 'undefined',
            })
        } else if (this.data.currentTab === 3) {
            wx.navigateTo({
                url: '/packageInsurance/pages/insurance-gtg-success/insurance-gtg-success?id=' + event.currentTarget.dataset.id + '&type=' + '',
            })
        } else if (this.data.currentTab === 4) {
            wx.navigateTo({
                url: '/packageInsurance/pages/insurance-gtg-success/insurance-gtg-success?id=' + event.currentTarget.dataset.id + '&type=contract',
            })
        }
    },
    onReady() {

    },
    callPhone(phoneNumber) {
        wx.makePhoneCall({
            phoneNumber: phoneNumber,
            success: (res) => {
                console.log(111, res)
            },
            fail: function (res) {
                console.log(222, res)
            }
        })
    },
    showPhone() {
        let arr = []
        if (this.data.phonelist.length !== 0) {
            this.data.phonelist.forEach((ele, index) => {
                arr.push(ele.phone + '-' + ele.name)
            })
        } else {
            arr.push('15950927000')
        }

        wx.showActionSheet({
            itemList: arr,
            success: (res) => {
                if (this.data.phonelist.length !== 0) {
                    this.callPhone(this.data.phonelist[res.tapIndex].phone + '')
                } else {
                    this.callPhone('15950927000')
                }
            },
            fail(res) {
                console.log(res.errMsg)
            }
        })
    },
    onOpenpopup() {
        this.setData({
            isup: true
        })
    },
    onClosePopup() {
        this.setData({
            isup: false
        })
    },
    onToSubscribe() {
        wx.navigateTo({
            url: '/pages/subscribe/subscribe',
        })
    },
    onShow() {
        getadphone().then((res) => {
            console.log(res, 'yfsudahiygiuyh')
            if (res.length === 0) {
                return
            } else {
                this.data.phonelist = res.map((ele, index) => {
                    return {
                        name: ele.salesName,
                        phone: ele.phone
                    }
                })
                this.setData({
                    phonelist: this.data.phonelist
                })
            }
        })
        this.getAccountProductInfo()
        this.getAccountExpireNotify()
    },
    // 提示信息
    onTips() {
        console.log('点击啦')
        this.setData({
            isShowTips: !this.data.isShowTips
        })
    },
    async getAccountProductInfo() {
        const result = await getAccountProductInfo()
        console.log(result, 'result')
        const percentage = result.pay.overTodayQuota / result.pay.totalTodayQuota * 100
        result.tyxSign.isOpen = !!result.tyxSign.isOpen
        this.setData({
            accountProductInfo: result,
            percentage: percentage,
            tyxSign: result.tyxSign.isOpen,
            isEleSign: result.eleSign.isOpen
        })
    },
    // 到期时间
    async getAccountExpireNotify() {
        const result = await getAccountExpireNotify()
        result.accountExpireTime = result.accountExpireTime ? dayjs(result.accountExpireTime).format('YYYY/MM/DD').replace(/\//g, '-') : null
        this.setData({
            expirationTime: result,
        })
    },
    onMyRecharge(e) {
        console.log(e, user.insureCompanyType)
        if (e.currentTarget.dataset.type === 'app') {
            wx.navigateToMiniProgram({
                appId: e.currentTarget.dataset.path,
            })
        } else {
            if (user.insureCompanyType === 'insure001_wesure') {
                toast('功能维护中')
                return
            } else {
                let str = e.currentTarget.dataset.type ? '?type=' + e.currentTarget.dataset.type : ''
                wx.navigateTo({
                    url: e.currentTarget.dataset.path + str
                })
            }

        }
    },
    // 充值
    onRecharge() {
        if (!this.data.isOpenPay) {
            app.toast('暂未开通发薪')
            return
        }
        wx.navigateTo({
            url: '/packagePay/pages/recharge/index/index'
        })
    },
    // 日日保充值
    onDayRecharge() {
        if (!user.isOpenWesure) {
            console.log('公司未开通日日保，请联系管理员开通后进行充值')
            // app.toast('公司未开通日日保，请联系管理员开通后进行充值')
            return
        }
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-recharge/insurance-recharge?title=日日保充值'
        })
    },
    // 短信充值
    onmassmessagerecharge() {
        wx.navigateTo({
            url: '/packageMassmessage/pages/message-recharge/message-recharge'
        })
    },
    onLongRecharge() {
        if (!user.isOpenPeriodInsur) {
            console.log('您暂未开通雇主保，请联系您的客服专员开通后再试')
            // app.toast('您暂未开通雇主保，请联系您的客服专员开通后再试')
            return
        }
        // 携带 longTerm参数 该参数是 说明 从长期保跳转过来的
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-recharge/insurance-recharge?type=InsureRechargePeriodZgrs&title=长期保充值'
        })
    },
    // 去团意保记录页面
    onMinuteRecharge() {
        wx.navigateTo({
            url: '/packageMicroInsurance/pages/insurance-recharge/insurance-recharge',
        })
    },
    onGoEleSignRecharge() {
        if (!this.data.accountProductInfo.eleSign.isOpen) {
            console.log('未开通电子签')
            return
        }
        // 去电子签包
        wx.navigateTo({
            url: '/packageContract/pages/contract-recharge/contract-recharge'
        })
    },
    // 去续费
    onGoRecharge() {
        // 携带 payRenew 参数 该参数是 说明 续费过来的
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-recharge-bullet/insurance-recharge-bullet?type=payRenew&title=发薪专户续费'
        })
    },
    // 去开通发薪
    goOpenPay() {
        this.setData({
            isPopupOpenPay: true
        })
    },
    onCancelLock() {
        this.setData({
            isPopupOpenPay: false
        })
    },
    scrollEnd() {
        console.log(111)
        if (!this.data.hasNext) {
            return
        }
        this.setData({
            page: this.data.pageNum += 1
        })
        if (this.data.currentTab === 1) {
            this.getIongInsuranceRechargeDetail()
        } else if (this.data.currentTab === 2) {
            this.onGetInsuranceRecharge()
        } else if (this.data.currentTab === 3) {
            this.onGetInsuranceRechargec()
        } else if (this.data.currentTab === 4) {
            this.getEleSignRechargeRecord()
        }
    }
})